package com.kuaimacode.kframework.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.sys.DictMapper;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 字典
 */
@Service
public class DictService extends ServiceImpl<DictMapper, Dict> {
    /**
     *  分页查找所有的数据
     * @param pageNum 当前页码，从1开始
     * @param pageSize  一页显示多少行
     * @return
     */
    public PageInfo<Dict> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Dict> datas = baseMapper.selectList(null);
        return new PageInfo<Dict>(datas);
    }
    /**
     * 根据字典标识获取字典数据
     * @param name 字典标识
     */
    public Dict getName(String name){
        QueryWrapper<Dict> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("name", name);
        qryWrapper.eq("level" , Constant.DictLevelEnum.FIRST.getCode());
        return baseMapper.selectOne(qryWrapper);
    }

    /**
     * 根据字典标识获取子字典数据
     * @param name 字典标识
     */
    public List<Dict> getChildren(String name){
        QueryWrapper<Dict> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("name", name);
        qryWrapper.eq("level" , Constant.DictLevelEnum.SECOND.getCode());
        return baseMapper.selectList(qryWrapper);
    }


    /**
     * 根据字典标识获取子字典数据
     * @param name 字典标识
     */
    public List<Dict> getChildrenByPId(String name,String pCode){
        QueryWrapper<Dict> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("name", name);
        qryWrapper.eq("code" , pCode);
        Dict dict = baseMapper.selectOne(qryWrapper);

        if(dict == null){
            return new ArrayList<Dict>();
        }
        QueryWrapper<Dict> qryWrapper1 = new QueryWrapper<>();
        qryWrapper1.eq("parent_id",dict.getId());
        qryWrapper1.eq("status",Constant.DictStatusEnum.NORMAL.getCode());
        return baseMapper.selectList(qryWrapper1);
    }



    public PageInfo<Dict> getChildrenPage(int pageNum, int pageSize,String name) {
        PageHelper.startPage(pageNum, pageSize);

        QueryWrapper<Dict> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("name", name);
        qryWrapper.ne("code", "1");
        qryWrapper.eq("level" , Constant.DictLevelEnum.SECOND.getCode());
        qryWrapper.orderByAsc("reserved");

        List<Dict> datas = baseMapper.selectList(qryWrapper);
        return new PageInfo<Dict>(datas);
    }

    //根据字典标识查询最大code
    public int getMaxCode(String name){
        QueryWrapper<Dict> qryWrapper = new QueryWrapper<>();
        HashMap maxMap = baseMapper.selectMaxCode(name);
        if(maxMap==null) return 0;
        return ((Long) maxMap.get("MAX_CODE")).intValue();
    }
    //校验字典表是否已经存在
    //
    public boolean checkExist(String name,String value,long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",name);
        queryWrapper.eq("value",value);
        if(id>0) queryWrapper.ne("id",id);
        return baseMapper.selectCount(queryWrapper) >0 ? true:false;
    }

    //根据字典类型 和 code 查询Dict
    public Dict getDictByNameAndCode(String name,String code){

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",name);
        queryWrapper.eq("code",code);
        return baseMapper.selectOne(queryWrapper);

    }



}

